<?

include_once(CONTROLLERS_PATH.'controller.php');
include_once(SSPATH.'menu.php');
include_once(SSPATH.'ssconfig.php');

$root = null;

function rusDate($mysqlDate) {
    return date('<b>d.m.Y</b> H:i:s', strtotime($mysqlDate));
}

class ss extends controller {
    protected $user;
    protected $request;
    protected $template;
    static public $task;
    
    function __construct($a_request, $template='index.html') {
        GLOBAL $_SESSION, $root;
        
        parent::__construct($a_request);
        $root = $this;
        $this->template = $template;
        if (isset($_SESSION['user'])) $this->setUser($_SESSION['user']);
    }
    
    protected function setUser($a_user) {
        $this->user = $a_user;
    }
    
    public static function getInstance() {
        GLOBAL $root;
        return $root;
    }
    
    public static function getTaskLink() {
        GLOBAL $_GET;
        return '?task='.ss::$task[0].','.ss::$task[1];//.($_GET['target']?('&target='.$_GET['target']):'').($_GET['dev']?('&dev='.$_GET['dev']):'');
    }
    
    public static function getCurrentProtocol() {
        return 'http://';
    }
    
    public static function getCurrentHost() {
        GLOBAL $_SERVER;
        return $_SERVER['HTTP_HOST'];
    }
    
    public static function getCurrentURL() {
        return ss::getCurrentProtocol().ss::getCurrentHost().ss::getTaskLink();
    } 
    
    public static function isCurrentTask($task) {
        $atask = explode(',', $task);
        $check = true;
        foreach (ss::$task as $key=>$elem)
            $check = $check && (isset($atask[$key]) && (ss::$task[$key] == $atask[$key]));
        return $check;
    }
    
    public static function setTemplate($templateName) {
        GLOBAL $root;
        $root->template = $templateName;
    }
    
    public function page() {
        $task = $this->request->getVar('task', $this->request->getVar('state')?'app,view':'article,viewArticle,home');
        ss::$task = explode(',', $task);
        ob_start();
        $controller = parent::redirect(ss::$task[0], (isset(ss::$task[1])?ss::$task[1]:ss::$task[0]));
        $content = ob_get_contents();
        ob_end_clean();
        require TEMPLATES_PATH.$this->template;
    }
}

?>